package model.expression.command;

import java.util.List;

import state.TabState;
import state.TurtleState;

import model.expression.AbstractExpression;

public class SetHeading extends AbstractCommandExpression{
	
	public SetHeading(TabState ts) {
		super(ts);
	}

	@Override
	public int evaluate() {
		int firstActiveTurtleReturnState = 0;
		int step = this.children.get(0).evaluate();
		// for each active turtle, evaluate
		for (int i = 0; i < tabSt.getActiveTurtle().size(); i++) {
			int turtleID = tabSt.getActiveTurtle().get(i);
			//for the first active turtle, record the return state
			if (i == 0){
				int originalAngle = tabSt.turtlesMap.get(turtleID).getOrientation();
				tabSt.turtlesMap.get(turtleID).setOrientation(step);
				firstActiveTurtleReturnState = Math.abs(originalAngle-step) > 180 ? 360-Math.abs(originalAngle-step): Math.abs(originalAngle-step);
			}
			else{
				tabSt.turtlesMap.get(turtleID).setOrientation(step);
			}
			tabSt.turtlesMap.get(turtleID).notifyObservers();
		}
		return firstActiveTurtleReturnState;
	}

	@Override
	protected int getExpNumChildren() {
		return 1;
	}

}

